/*
 * Copyright 2016 - 2018 NXP
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * File: s32k1xx_peripheries.h
 *
 * This file implements functions for LPSPI and GPIO operations required by BCC
 * driver. It is closely related to this demo example.
 */
#include "../MC3377X_USER.h"
#ifdef MC33771C

#ifndef BCC_PERIPHERIES_H_
#define BCC_PERIPHERIES_H_


#include "mc33771c.h"
#include <S32K146.h>

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*!
 * @addtogroup macro_group
 * @{
 */

/*!< Gets needed cycles for specified delay in milliseconds, calculation is based on core clock frequency. */
#define BCC_GET_CYCLES_FOR_MS(ms, freq) (((freq) / 1000U) * (ms))

/*!< Gets needed cycles for specified delay in microseconds, calculation is based on core clock frequency. */
#define BCC_GET_CYCLES_FOR_US(us, freq) (((freq) / 1000U) * (us) / 1000U)
/*! @} */

/*! @brief Timeout used for SPI/TPL communication. */

/* EN - PTC9 (TPL only) */
#define BCC_EN_INSTANCE           PORTD
#define BCC_EN_INDEX              1

/* INTB - PTC14 (TPL only) */
#define BCC_INTB_INSTANCE         PORTE
#define BCC_INTB_INDEX            3

/* RST - PTD4 (SPI only) */
#define BCC_RST_INSTANCE          0//PORTD
#define BCC_RST_INDEX             4

/* LPSPI configuration (SPI only). */
#define BCC_SPI_LPSPI_INSTANCE    0 /* LPSPI0 */
#define BCC_SPI_LPSPI_BAUD        1000000
#define BCC_SPI_LPSPI_SRCCLK      1//lpspiCom3_MasterConfig0.lpspiSrcClk
/* CSB - PTB5 */
#define BCC_SPI_CSB_INSTANCE      PORTA
#define BCC_SPI_CSB_PIN           9

/* LPSPI_TX configuration (TPL only). */
#define BCC_TPL_TX_LPSPI_INSTANCE 2//0 /* LPSPI1 */
#define BCC_TPL_TX_LPSPI_BAUD     1500000//1500000
#define BCC_TPL_TX_LPSPI_SRCCLK   lpspiCom3_MasterConfig0.lpspiSrcClk
/* CSB_TX - PTE9 */
#define BCC_TPL_TX_CSB_INSTANCE   PORTA
#define BCC_TPL_TX_CSB_PIN        9

/* LPSPI_RX configuration (TPL only). */
#define BCC_TPL_RX_LPSPI_INSTANCE 0//1 /* LPSPI0 */
#define BCC_TPL_RX_CSB            LPSPI_PCS1//LPSPI_PCS0 /* CSB_RX - PTD3 (LPSPI1_PCS0) */

/*! @} */

/*******************************************************************************
 * API
 ******************************************************************************/
/*!
 * @addtogroup function_group
 * @{
 */

/*!
 * @brief This function configures LPSPI for usage with BCC driver.
 */

/*!
 * @brief This function sends and receives data via SPI bus. Intended for SPI
 * mode only.
 *
 * @param drvInstance Instance of BCC driver.
 * @param transBuf Pointer to data to be sent.
 * @param recvBuf Pointer to buffer for received data.
 * @param transferByteCount Number of bytes to be transfered.
 *
 * @return bcc_status_t Error code.
 */
bcc_status_t BCC_TransferSpi(uint8_t drvInstance, uint8_t transBuf[],
        uint8_t recvBuf[], uint16_t transferByteCount);

/*!
 * @brief This function sends and receives data via TX and RX SPI buses.
 * Intended for TPL mode only.
 *
 * @param drvInstance Instance of BCC driver.
 * @param transBuf Pointer to data to be sent.
 * @param recvBuf Pointer to buffer for received data.
 * @param transBufLen Number of bytes to be transceived.
 * @param recvBufLen Number of bytes to be received.
 *
 * @return bcc_status_t Error code.
 */
extern bcc_status_t BCC_TransferTpl(uint8_t drvInstance, uint8_t transBuf[],
        uint8_t recvBuf[], uint16_t transBufLen, uint16_t recvBufLen);


/*!
 * @brief Waits for specified amount of milliseconds.
 *
 * @param delay - Number of milliseconds to wait.
 */
void BCC_WaitMs(uint16_t delay);

/*!
 * @brief Waits for specified amount of microseconds.
 *
 * @param delay - Number of microseconds to wait.
 */
void BCC_WaitUs(uint16_t delay);


void BCC_GPIO2CS(void);
void BCC_CS2GPIO(void);

/*!
 * @brief Writes logic 0 or 1 to the CSB pin (or CSB_TX in case of TPL mode).
 * This function needs to be implemented by the user.
 *
 * @param drvInstance Instance of BCC driver.
 * @param value - Zero or one to be set to CSB (CSB_TX) pin.
 */
void BCC_WriteCsbPin(uint8_t drvInstance, uint8_t value);

/*!
 * @brief Writes logic 0 or 1 to the RST pin.
 *
 * @param drvInstance Instance of BCC driver.
 * @param value - Zero or one to be set to RST pin.
 */
void BCC_WriteRstPin(uint8_t drvInstance, uint8_t value);

/*!
 * @brief Writes logic 0 or 1 to the EN pin of MC33664.
 *
 * @param drvInstance Instance of BCC driver.
 * @param value - Zero or one to be set to EN pin.
 */
void BCC_WriteEnPin(uint8_t drvInstance, uint8_t value);

/*!
 * @brief Reads logic value of INTB pin of MC33664.
 *
 * @param drvInstance Instance of BCC driver.
 *
 * @return Zero value for logic zero, non-zero value otherwise.
 */
extern uint32_t BCC_ReadIntbPin(uint8_t drvInstance);
/*! @} */

/*!
 * @brief Returns SCG system clock frequency.
 *
 * @return SCG system clock frequency.
 */

/*!
 * @brief Waits for specified amount of cycles which is given by 32bit
 *        value range. Assumption for this function is that target
 *        architecture is using 32bit general purpose registers.
 *
 * @param cycles - Number of cycles to wait.
 */
inline void BCC_WaitCycles(uint32_t cycles);

#endif /* BCC_PERIPHERIES_H_ */
#endif
/*******************************************************************************
 * EOF
 ******************************************************************************/
